/*
 * CJSRedis.h
 *
 *  Created on: Apr 11, 2017
 *      Author: root
 */

#ifndef CJSREDIS_H_
#define CJSREDIS_H_

#include <iostream>
#include <string>
#include <stdio.h>
#include <vector>
#include <queue>
#include <hiredis/hiredis.h>
#include <boost/shared_ptr.hpp>
#include "redis_struct.h"

class CJSRedis {
public:
	CJSRedis();
	virtual ~CJSRedis();

	//设置redis连接参数
	void SetConnectParam(const T_JS_Redis_Connect_Param* pIn);
    //连接redis
    int Connect();

    //检测当前连接是否生效
    bool IsConnectValid();

    //删除当前数据库的所有信息（根据库号）
    bool TruncateCurrDB();

    //检测key是否存在
    bool Exists(std::string strKey);
    //设置key超时
    bool Expireat(std::string strKey, time_t msUTC);

    //string oper
    bool Get(std::string strKey,std::string& strOut);
    void Set(std::string strKey, std::string strValue);


    //hash oper
    bool H_Exists(std::string strKey, std::string strField, bool& bExist);
    bool H_Set(std::string strKey, std::string strField, std::string strValue);
    bool H_Get(std::string strKey, std::string strField, std::string& strOut);
    bool H_MSet(std::string strKey, CJS_STR_MAP_KEY_VALUE& mapIn);
    bool H_MGet(std::string strKey, CJS_STR_MAP_KEY_VALUE& mapIn);
    bool H_GetAll(std::string strKey, CJS_STR_MAP_KEY_VALUE& mapOut);

    //list oper
    //头部（左边）插入一个元素
    bool L_Push(std::string strKey,std::string strValue);
    //尾部（右边）弹出并获取一个元素
    bool BR_POP(std::string strKey,int nTTL,std::string& strOut);
    //通过索引获取列表中的元素
    bool L_Index(std::string strKey, int nIdx, std::string& strOut);
    //获取列表指定范围内的元素
    bool L_Range(std::string strKey, int nStart, int nStop, CStringArray& straOut);
    //list长度
    bool L_Len(std::string strKey, int& nOut);

    //set oper
    //向有序集合strKey增加一个成员member
    bool ZAdd(std::string strKey,int nScore,std::string member);
    //删除有序集合strKey里一个成员member
    bool ZRem(std::string strKey,std::string member);
    //获取某个用户的排名 nRank:返回排名
    bool ZRevrank(std::string strKey,std::string member,int& nRank);
    //返回有序集合strKey里成员member的分数
    bool ZScore(std::string strKey,std::string member,int& nScore);
    //返回有序集合strKey里索引为nStart~nStop的所有成员
    bool ZRange(std::string strKey,int nStart,int nStop,std::vector<std::string>& rangOut);
    //返回有序集合strKey里分数满足区间nStart~nStop的所有成员
    bool ZRangeByScore(std::string strKey,int nStart,int nStop,std::vector<std::string>& rangOut);
    //给有序集合strKey的成员分数member加上增量increment
    bool ZIncrby(std::string strKey,int increment,std::string member);


private:
    redisContext* _connect;
    redisReply* _reply;
    T_JS_Redis_Connect_Param m_tConnectParam;
};

#endif /* CJSREDIS_H_ */
